package com.beerq.view.friends;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.beerq.R;
import com.beerq.util.AppConfig;
import com.beerq.util.BasicHttp;
import com.beerq.util.Constants;
import com.beerq.util.StringUtil;
import com.beerq.view.BaseActivity;
import com.beerq.view_manager.AddImageManager;
import com.beerq.view_manager.CommonManager;

public class PostInfoActivity extends BaseActivity {


    private String base64String="";
    AddImageManager mAddImageManager;

    private TextView mTxtContent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarType=0;
        super.onCreate(savedInstanceState);

        setMainContentView(R.layout.activity_post_info);
        hideTopBar();

        mTxtContent=(TextView)findViewById(R.id.txt_content);

        mAddImageManager=new AddImageManager(this);
        mAddImageManager.getImages().clear();
        mAddImageManager.bind();
        setEvent();
    }

    private void setEvent(){

        findViewById(R.id.txt_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.txt_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=mTxtContent.getText().toString();
                if(StringUtil.isEmpty(content)){
                    showToast("啤气内容不能为空");
                    return;
                }

                if(mAddImageManager.getImages().size()>0){
                    showLoadingDialog(false);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String path=mAddImageManager.getImages().get(0).path;
                            base64String= AppConfig.compressImageReturnBase64(path);
//                            base64String=  CommonManager.bitmap2StrByBase64(bitmap,mAddImageManager.getImages().get(0).path);
                            Message message= Message.obtain();
                            message.what=1;
                            mHandler.sendMessage(message);

                        }
                    }).start();
                }else{
                    showToast("啤气图片不能为空");
                    return;
//                    postInfo(mTxtContent.getText().toString(),"");
                }
            }
        });
    }

    Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==0){
                finish();
            }else if(msg.what==1){
                CommonManager.uploadImage(mContext, mAddImageManager.getImages().get(0).path, base64String, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(String resonseData) {
                        JSONObject jsonObject=JSONObject.parseObject(resonseData);
                        postInfo(mTxtContent.getText().toString(),jsonObject.getJSONObject("Result").getString("FileFullUrl"));
                    }
                    @Override
                    public void onFailure(String resonseData) {

                    }
                });
            }
            return false;
        }
    });



    @Override
    protected void onResume() {
        super.onResume();
        mAddImageManager.refresh();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mAddImageManager.onActivityResult(requestCode,resultCode,data);
    }

    private void postInfo(String content,String imgUrl){


        FriendsManager.postInfo(mContext, Constants.userInfo.getAppUserId(), content, imgUrl, new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(String resonseData) {
                showToast("发布成功！");
                mHandler.sendEmptyMessageDelayed(0,1000);
            }

            @Override
            public void onFailure(String resonseData) {

            }
        });
    }
}
